package schronisko;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class Query {
	Statement st = MySQLConnection.connect();
	
	public int Insert(String table, String q) throws SQLException {
		int row =  st.executeUpdate("INSERT INTO " +table+" VALUES(" +  q + ")");
		//MySQLConnection.Close();
		return row;
	}
	
	public ResultSet Delete(String table, String where, Object value) throws SQLException {
		ResultSet result = null;
		result = st.executeQuery("DELETE FROM "+ table + " WHERE " + where +"= '" + value + "'");
		//MySQLConnection.Close();
		return result;
	}
	
	public ResultSet Select(String fields, String table, String where, Object value) throws SQLException {
		ResultSet result = null;
		result = st.executeQuery("SELECT " + fields + " FROM " + table +
				" WHERE " + where +"= '" + value + "'");
		//MySQLConnection.Close();
		return result;
	}
	public ResultSet Select2(String fields, String table, String where) throws SQLException {
		ResultSet result = null;
		result = st.executeQuery("SELECT " + fields + " FROM " + table +
				" WHERE " + where +"< 2");
		//MySQLConnection.Close();
		return result;
	}
	public ResultSet Select_2OR(String fields, String table, String where, Object value, String where2, Object value2) throws SQLException {
		ResultSet result = null;
		result = st.executeQuery("SELECT " + fields + " FROM " + table +
				" WHERE " + where +" = '" + value + "' OR " + where2 + "= '" + value2 +"'");
		//MySQLConnection.Close();
		return result;
	}
	public ResultSet Select_AND(String fields, String table, String where, Object value, String where2, Object value2) throws SQLException {
		ResultSet result = null;
		result = st.executeQuery("SELECT " + fields + " FROM " + table +
				" WHERE " + where +" = '" + value + "' AND " + where2 + "= '" + value2 + "'");
		//MySQLConnection.Close();
		return result;
	}
	public ResultSet Select_3AND(String fields, String table, String where, Object value, String where2, Object value2, String where3, Object value3, String eq, String where4, Object value4) throws SQLException {
		ResultSet result = null;
		result = st.executeQuery("SELECT " + fields + " FROM " + table +
				" WHERE " + where +" = '" + value + "' AND " + where2 + "= '" + value2 + "' AND " + where3 + " " +  eq + " '" + value3 +"' AND " + where4 + "= '" + value4 + "'");
		//MySQLConnection.Close();
		return result;
	}
	public ResultSet SelectBetween(String fields, String table, String where, String value1, String value2) throws SQLException {
		ResultSet result = null;
		result = st.executeQuery("SELECT " + fields + " FROM " + table +
				" WHERE " + where +" between " + value1 + " and "+ value2);
		//MySQLConnection.Close();
		return result;
	}
	
	public ResultSet SelectGroupedBy(String fields, String table, String where, Object value, String group) throws SQLException {
		ResultSet result = null;
		result = st.executeQuery("SELECT " + fields + " FROM " + table +
				" WHERE " + where + " = " + value + " GROUP BY " + group);
		return result;
	}
	public ResultSet Select_2AND_Less(String fields, String table, String where, String where2, Object value) throws SQLException {
		ResultSet result = null;
		result = st.executeQuery("SELECT " + fields + " FROM " + table +
				" WHERE (" + where +")> 2 AND " + where2 + "= '" + value + "'");
		//MySQLConnection.Close();
		return result;
	}
	
}
